/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.dao;

import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.transaction.UserTransaction;
import model.dao.exceptions.NonexistentEntityException;
import model.pojo.Disciplina;
import model.pojo.Aluno;
import model.pojo.Historico;

/**
 *
 * @author leonidia
 */
public class HistoricoJpaController implements HistoricoInterface {

    public HistoricoJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    @Override
    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    @Override
    public void create(Historico historico) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Disciplina disciplina = historico.getDisciplina();
            if (disciplina != null) {
                disciplina = em.getReference(disciplina.getClass(), disciplina.getId());
                historico.setDisciplina(disciplina);
            }
            Aluno aluno = historico.getAluno();
            if (aluno != null) {
                aluno = em.getReference(aluno.getClass(), aluno.getId());
                historico.setAluno(aluno);
            }
            em.persist(historico);
            if (disciplina != null) {
                disciplina.getHistorico().add(historico);
                disciplina = em.merge(disciplina);
            }
            if (aluno != null) {
                aluno.getHistorico().add(historico);
                aluno = em.merge(aluno);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    @Override
    public void edit(Historico historico) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Historico persistentHistorico = em.find(Historico.class, historico.getId());
            Disciplina disciplinaOld = persistentHistorico.getDisciplina();
            Disciplina disciplinaNew = historico.getDisciplina();
            Aluno alunoOld = persistentHistorico.getAluno();
            Aluno alunoNew = historico.getAluno();
            if (disciplinaNew != null) {
                disciplinaNew = em.getReference(disciplinaNew.getClass(), disciplinaNew.getId());
                historico.setDisciplina(disciplinaNew);
            }
            if (alunoNew != null) {
                alunoNew = em.getReference(alunoNew.getClass(), alunoNew.getId());
                historico.setAluno(alunoNew);
            }
            historico = em.merge(historico);
            if (disciplinaOld != null && !disciplinaOld.equals(disciplinaNew)) {
                disciplinaOld.getHistorico().remove(historico);
                disciplinaOld = em.merge(disciplinaOld);
            }
            if (disciplinaNew != null && !disciplinaNew.equals(disciplinaOld)) {
                disciplinaNew.getHistorico().add(historico);
                disciplinaNew = em.merge(disciplinaNew);
            }
            if (alunoOld != null && !alunoOld.equals(alunoNew)) {
                alunoOld.getHistorico().remove(historico);
                alunoOld = em.merge(alunoOld);
            }
            if (alunoNew != null && !alunoNew.equals(alunoOld)) {
                alunoNew.getHistorico().add(historico);
                alunoNew = em.merge(alunoNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = historico.getId();
                if (findHistorico(id) == null) {
                    throw new NonexistentEntityException("The historico with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    @Override
    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Historico historico;
            try {
                historico = em.getReference(Historico.class, id);
                historico.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The historico with id " + id + " no longer exists.", enfe);
            }
            Disciplina disciplina = historico.getDisciplina();
            if (disciplina != null) {
                disciplina.getHistorico().remove(historico);
                disciplina = em.merge(disciplina);
            }
            Aluno aluno = historico.getAluno();
            if (aluno != null) {
                aluno.getHistorico().remove(historico);
                aluno = em.merge(aluno);
            }
            em.remove(historico);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    @Override
    public List<Historico> findHistoricoEntities() {
        return findHistoricoEntities(true, -1, -1);
    }

    @Override
    public List<Historico> findHistoricoEntities(int maxResults, int firstResult) {
        return findHistoricoEntities(false, maxResults, firstResult);
    }

    private List<Historico> findHistoricoEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Historico as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    @Override
    public Historico findHistorico(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Historico.class, id);
        } finally {
            em.close();
        }
    }

    @Override
    public int getHistoricoCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from Historico as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
